home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14358 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  56 lines

  1. Path: osf1.gmu.edu!rraffer1
  2. From: rraffer1@osf1.gmu.edu (Ryan M Rafferty)
  3. Newsgroups: comp.lang.c++
  4. Subject: Bletcherous kludge!
  5. Date: 29 Mar 1996 23:17:31 GMT
  6. Organization: George Mason University, Fairfax, Virginia, USA
  7. Message-ID: <4jhr2b$9ou@portal.gmu.edu>
  8. NNTP-Posting-Host: osf1.gmu.edu
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=US-ASCII
  11. Content-Transfer-Encoding: 7bit
  12. X-Newsreader: TIN [version 1.2 PL2]
  13.  
  14. I am trying to get the contents of a custom String class to be converted
  15. into numeric data, ie float.  The String class consists of :
  16.  
  17. Buffer:  a null-terminated array of char
  18.  
  19. and several methods for common string stuff, including overloaded << and >>
  20. operators.  The code listed below is the only method I've been able to come up
  21. with that works, but it is really an awful hack that outputs the contents of
  22. the String instance to a file and then reads it back in to do the conversion.
  23.  
  24. I tried creating an instance of iostream, inputting to it from the String
  25. instance, and then outputting from it into a float variable, but it just
  26. didn't work.  Any suggestions on how to do this the right way would be
  27. greatly appreciated!
  28.  
  29. Ryan Rafferty 
  30.  
  31. #include "String.h"
  32.  
  33. float String::Number() {
  34.  
  35.   float i = 0;
  36.  
  37.   // this conversion method is, in its current state, a HORRIBLE and
  38.   // AWFUL kludge to convert the contents of a String instance to a
  39.   // numeric type.  It doesn't do any error checking, but what's worse,
  40.   // it USES A TEMPORARY FILE !!!! I'm so mad at myself!  But I can't
  41.   // seem to be able to bend the will of the iostreams class to do what
  42.   // I want!
  43.  
  44.   ofstream fout("./__temp");
  45.   fout << Buffer;
  46.   fout.close();
  47.   ifstream fin("./__temp");
  48.   fin >> i;
  49.   fin.close();
  50.  
  51.   //      YECH!  A feeble attempt to hide my shame...
  52.   //      execl("/bin/rm", "rm", "./__temp", (char *) 0);
  53.  
  54.   return i;
  55. };
  56.